package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;

public class ContactHelper extends HelperBase {
	

	public ContactHelper(ApplicationManager manager) {
		super(manager);		
	}

	private List<ContactData> cachedContacts;
	
	public List<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}	
		return cachedContacts;
	}
	
	private void rebuildCache() {
		List<ContactData> cachedContacts = new ArrayList<ContactData>();
		manager.navigateTo().mainPage();
		WebElement tbody = driver.findElement(By.xpath("//*[@id='maintable']/tbody"));
		List<WebElement> rows = tbody.findElements(By.name("entry"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td"));
			String lastname = cells.get(1).getText();
			String firstname = cells.get(2).getText();
			String email = cells.get(3).findElement(By.tagName("a")).getText();
			String homephone = cells.get(4).getText();
			ContactData contact = new ContactData().withLastname(lastname).withFirstname(firstname).withEmail(email).withHomephone(homephone);
			cachedContacts.add(contact);
		}
	}

	public ContactHelper createContact(ContactData contact) {
		initContactCreation();
		fillContactForm(contact);
		submitContactCreation();
		returnToContactsPage();	
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact) {
		initContactModification(index);
		fillContactForm(contact);
		submitContactModification();
		returnToContactsPage();
		rebuildCache();
		return this;
	}

	public ContactHelper contactDeletion(int index) {
		initContactModification(index);
		deleteContact();
		returnToContactsPage();
		rebuildCache();
		return this;
	}	
	
	//-----------------------------------------------
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact) {
		type("firstname", contact.getFirstname());
		type("lastname", contact.getLastname());
		type("address", contact.getAddress());
		type("home", contact.getHomephone());
		type("mobile", contact.getMobilephone());
		type("work", contact.getWorkphone());
		type("email", contact.getEmail());
		type("email2", contact.getEmail2());
		selectByText(By.name("bday"), contact.getDate());
		selectByText(By.name("bmonth"), contact.getMonth());
		type("byear", contact.getYear());
		type("address2", contact.getAddress2());
		type("phone2", contact.getPhone2());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper returnToContactsPage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactModification(int index) {
		click(By.xpath(".//*[@id='maintable']//tr[" + (index+2) + "]/td[7]//img"));
		return this;
	}

	public ContactHelper deleteContact() {
		click(By.xpath("//div[@id='content']/form[2]/input[2]"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//div[@id='content']/form/input[11]"));
		cachedContacts = null;
		return this;
	}
}
